
import org.apache.spark.SparkConf
import org.apache.spark.sql.{SaveMode, SparkSession}
/**
 * 分析之后的数据同步到MySQL
 */
object mysql {
  def main(args: Array[String]): Unit = {
    //1. Spark配置对象
    val sparkConf = new SparkConf()
      .setMaster("local[*]")
    //2. SparkSession对象
    val sparkSession = SparkSession
      .builder()
      .enableHiveSupport() //-开启hive的支持
      .config(sparkConf)
      .appName("mysql")
      .getOrCreate()
    //3. 读取hive中对应表的内容
    val fav_name = sparkSession.sql("select * from db_minsu.fav_name")
    val layout_num = sparkSession.sql("select * from db_minsu.layout_num")
    val district_num = sparkSession.sql("select * from db_minsu.district_num ")
    val price_num = sparkSession.sql("select * from db_minsu.price_num")
    val rat_num = sparkSession.sql("select * from db_minsu.rat_num")
    val tag_num = sparkSession.sql("select * from db_minsu.tag_num")

    //4. 将读取到的内容，导出到MySQL数据库中
    fav_name.write
      .format("jdbc")
      .option("url","jdbc:mysql://192.168.244.130:3306/minsudb")
      .option("user","root")
      .option("password","root")
      .option("dbtable","fav_name")
      .mode(SaveMode.Append)
      .save()
    layout_num.write
      .format("jdbc")
      .option("url","jdbc:mysql://192.168.244.130:3306/minsudb")
      .option("user","root")
      .option("password","root")
      .option("dbtable","layout_num")
      .mode(SaveMode.Append)
      .save()
    district_num.write
      .format("jdbc")
      .option("url","jdbc:mysql://192.168.244.130:3306/minsudb")
      .option("user","root")
      .option("password","root")
      .option("dbtable","district_num")
      .mode(SaveMode.Append)
      .save()
    price_num.write
      .format("jdbc")
      .option("url","jdbc:mysql://192.168.244.130:3306/minsudb")
      .option("user","root")
      .option("password","root")
      .option("dbtable","price_num")
      .mode(SaveMode.Append)
      .save()
    rat_num.write
      .format("jdbc")
      .option("url","jdbc:mysql://192.168.244.130:3306/minsudb")
      .option("user","root")
      .option("password","root")
      .option("dbtable","rat_num")
      .mode(SaveMode.Append)
      .save()
    tag_num.write
      .format("jdbc")
      .option("url","jdbc:mysql://192.168.244.130:3306/minsudb")
      .option("user","root")
      .option("password","root")
      .option("dbtable","tag_num")
      .mode(SaveMode.Append)
      .save()
    //-资源的释放
    sparkSession.stop()
    sparkSession.close()
  }
}